<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" class="mr-5" placeholder="请输入文件名称" suffix-icon="el-icon-search"
                v-model="name"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button class="ml-5" type="warning" @click="reSet">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-upload action="http://localhost:9876/sys-file/upload" accept="xlsx" :on-success="handelUpload"
                 style="display: inline-block">
        <el-button type="info" plain><i class="el-icon-upload2" style="margin-right: 5px"/>上传文件</el-button>
      </el-upload>
      <el-button type="danger" plain @click="batchDelete" class="ml-5"><i class="el-icon-remove-outline"
                                                                          style="margin-right: 5px"/>批量删除
      </el-button>

    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="headerBg"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="name" label="文件名称"></el-table-column>
      <el-table-column prop="type" label="文件类型"></el-table-column>
      <el-table-column prop="size" label="文件大小(kb)"></el-table-column>
      <el-table-column label="预览">
        <template slot-scope="scope">
          <el-button type="primary" @click="preview(scope.row.url)">预览</el-button>
        </template>
      </el-table-column>
      <el-table-column label="下载">
        <template slot-scope="scope">
          <el-button type="primary" @click="download(scope.row.url)">下载</el-button>
        </template>
      </el-table-column>
      <el-table-column label="启用">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.enable" active-color="#13ce66" inactive-color="#ccc"
                     @change="changeEnable(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="danger" plain class="el-icon-remove-outline" @click="handleRemove(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>

</template>

<script>
export default {
  name: "File",
  data() {
    return {
      headerBg: "headerBg",
      name: "",
      tableData: [],
      fileIds: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
    }
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      this.request.get("/sys-file/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name,
        }
      }).then(res => {
        if (res.code === 200) {
          this.tableData = res.data.records;
          this.total = res.data.total;
        }
      })
    },
    changeEnable(row) {
      this.request.post("/sys-file/update", row).then(res => {
        if (res.code === 200) {
          this.$message.success("操作成功！");
        }
      })
    },
    download(url) {
      window.open(url);
    },
    submitForm() {
      this.request.post("/user", this.from).then(res => {
        this.dialogFormVisible = false;
        if (res.code === 200) {
          this.$message.success("保存成功");
          this.load();
        } else {
          this.$message.error("保存失败");
        }
      })
    },
    handleRemove(id) {
      this.$confirm('是否确认删除编号为"' + id + '"的数据项？').then(() => {
        this.request.delete("/sys-file/" + id).then(res => {
          if (res.code === 200) {
            this.$message.success("删除成功");
            this.load();
          } else {
            this.$message.error("删除失败");
          }
        })
      })
    },
    handleSelectionChange(val) {
      console.log(val);
      this.fileIds = val;
    },
    batchDelete() {
      let ids = this.fileIds.map(v => v.id);
      this.$confirm('是否确认删除这些数据？').then(() => {
        this.request.post("/sys-file/batchDelete", ids).then(res => {
          if (res.code === 200) {
            this.$message.success("批量删除成功");
            this.load();
          } else {
            this.$message.error("删除失败");
          }
        })
      })
    },
    preview(url) {
      window.open('https://file.keking.cn/onlinePreview?url=' + encodeURIComponent(window.btoa((url))))
    },
    handelUpload() {
      this.$message.success("上传成功！");
      this.load();
    },
    reSet() {
      this.name = "";
      this.load();
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.load();
    }
  }
}
</script>

<style scoped>
.headerBg {
  background-color: #eee !important;
}
</style>
